package ListTest;

import java.util.Arrays;

public class MyArrayList implements Ilist{
    public int[] array;
    public int useSize;

    public static final int DEFAULT_CAPACITY = 10;
    //构造方法
    public MyArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }
    private void grow(){
        this.array = Arrays.copyOf(this.array,this.array.length*2);
    }
    @Override
    public void add(int data) {
        //1.判断容量
          if(this.ifFull()) this.grow();
        //2.增加数据
        this.array[useSize] = data;
        //3.useSize++
        this.useSize++;
    }

    @Override
    public boolean ifFull() {
        return this.useSize == array.length;
    }
    private void cheakPos(int pos){
        //判断是否为非法位置
        if(pos < 0 || pos > this.useSize) throw new PosException("位置错误异常！！！");
    }

    @Override
    public void add(int pos, int data) {
          //pos可能存在异常，需要捕获
        try{
            cheakPos(pos);//位置是否合法
            if(this.ifFull())//是否需要扩容
                  this.grow();
            for (int i = this.useSize; i >= pos; i--) {
                this.array[i] = this.array[i - 1];//挪位
            }
            this.array[pos] = data;//将数据赋值给指定位置的下标元素
            this.useSize++;//实际数量增加
        }catch (PosException e){
            e.printStackTrace();//抛出异常原因
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(this.array[i] == toFind) return true;
        }
        return  false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(this.array[i] == toFind) return i;
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        //pos要检测是否存在异常
        try{
            if(pos < 0 || pos >= useSize) throw new PosException("位置非法！！！");
            //位置合法，返回指定下标元素
            return this.array[pos];
        }catch (PosException e){
            e.printStackTrace();
        }
        return -1;
    }
    private boolean cheakEmpty(){
       return  this.useSize == 0;
    }
    @Override
    public void set(int pos ,int value) {
       try{
           cheakPos(pos);//位置不能非法
           cheakEmpty();//容量不能为0
           this.array[pos] = value;//赋值
       }catch (PosException e){
           e.printStackTrace();//输出异常原因
       }catch (EmptyException e){
           e.printStackTrace();//输出异常原因
       }
    }

    @Override
    public void remove(int toRemove) {
        int pos = -1;
        for (int i = 0; i < this.useSize; i++) {
           if( this.array[i] == toRemove) {
               pos = i;
               break;
           }
        }
        if(pos != this.useSize){
            if(pos == this.useSize) {this.array[pos] = 0;return;}
        for (int i = pos; i < this.useSize - 1; i++) {
            this.array[i] = this.array[i+1];
           }
          this.useSize--;//数量减一
        }
        else System.out.println("指定查找的元素不存在！！！");
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        this.array = null;
        useSize = 0;
    }

    @Override
    public void display() {
        System.out.print("[ ");
        for (int i = 0; i < useSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println("]");
    }
}
